/**
 * 
 */
package test.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import quickstart.model.Person;

import test.pojo.Blog;

/**
 * @author Ka
 *
 */
@Transactional
public class BlogServiceImpl implements BlogService {

	private EntityManager em;
	
	@PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }  
	
	@SuppressWarnings("unchecked")
    public List<Blog> findAll() {
        Query query = getEntityManager().createQuery("select b FROM Blog b");
        return query.getResultList();
    }
	
    public void save(Blog blog) {
        if (blog.getId() == null) {
            // new
            em.persist(blog);
        } else {
            // update
            em.merge(blog);
        }
    }
    
    public void remove(int id) {
    	Blog blog = find(id);
        if (blog != null) {
            em.remove(blog);
        }
    }
    
    private EntityManager getEntityManager() {
        return em;
    }
    
    public Blog find(int id) {
        return em.find(Blog.class, id);
    }
}
